/**
 * 获得搜索参数
 * @param params
 * @returns {Object}
 */
function getSearchParams(params) {
    var searchParams = new Object();
    if (params != undefined) {
        searchParams = params;
    }
    $('.SearchForm').each(function () {
        var param = $(this).val().trim();
        if (param == undefined)
            param = '';
        searchParams[$(this).attr('id')] = param;
    });
    return searchParams;
}


/**
 * 页面自加载
 */
$(function () {
    var cellwidth = ($(".box-content.table-responsive").width() - 55) / 11;
    var psval = $('#datatable_projecttype').attr('data-size');
    if (psval == undefined || psval == "") {
        psval = 10;
    }
    /**
     * easyui 生成表格数据
     */
    $('#datatable_projecttype').datagrid({
        striped: true,
        idField:'pt_id',
        remoteSort: false,
        collapsible: true,
        singleSelect:true,
        fit: false,
        url: 'admin/Project/getProjectType',//数据源路径
        loadMsg: '请等待数据载入....',
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        pageSize: psval,
        pageList: [psval, psval * 2, psval * 3, psval * 4, psval * 5],
        columns: [[{
            field: 'ptname',
            title: '类型名称',
            align: 'center',
            width: cellwidth,
            sortable: true
        },{
            field: 'pt_name',
            title: '步骤名称',
            align: 'center',
            width: cellwidth,
            sortable: true
        },{
            field: 'pt_id',
            title: '操作',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function(value,row,index){
                return "<button id='"+row.pt_id+"' onclick='toadd(this);' style='background-color: cornflowerblue;color: #FFF;'>添加此步骤任务</button>" +
                    "<button id='"+row.pt_id+"' onclick='tosee(this);' style='background-color: cornflowerblue;color: #FFF;margin-left: 10px'>查看任务</button>";
            }
        }
		]],
        onBeforeLoad: function (param) {
            param = getSearchParams(param);
        },
    });

    $('#Search').click(function () {
        $('#datatable_projecttype').datagrid('reload');
    });
    // 添加
    // $('.addbook').click(function(){
    //     var content = "";
    //     $.ajax({
    //         url: 'admin/course/addcourse',
    //         type: 'POST',
    //         dataType: 'HTML',//返回的数据类型
    //         success: function (updatemodalhtml) {
    //             content = updatemodalhtml;
    //             //弹出添加框
    //             $.TeachDialog({
    //                 modalId: null,
    //                 animation: null,
    //                 largeSize:true,
    //                 title: '添加课程',
    //                 content: content,
    //                 showCloseButton: true,
    //                 showCloseButtonName: '关闭',
    //                 CloseButtonAddFunc: function () {
    //                 },
    //                 otherButtons: ['添加'],
    //                 otherButtonStyles: ['btn-primary'],
    //
    //
    //                 clickButton: function (sender, modal, index) {
    //                     $.ajax({
    //                         url: 'admin/Course/insert',
    //                         data: $("#addForm").serialize(),
    //                         type: 'POST',
    //                         dataType: 'JSON',
    //                         success: function (returnData) {
    //                             if (returnData.status == 0) {
    //                                 $.TeachDialog({
    //                                     content: returnData.msg,
    //
    //                                 });
    //                             } else {
    //                                 modal.modal('hide');
    //                                 $.TeachDialog({
    //                                     content: returnData.msg,
    //                                     showCloseButton: false,
    //                                 });
    //                                 $('#datatable_projecttype').datagrid('reload');
    //                             }
    //                         },
    //                         error: function () {
    //                             $.TeachDialog({
    //                                 content: '系统异常，请联系管理员',
    //                             });
    //                         },
    //
    //                     });
    //
    //                 },
    //             });
    //         },
    //         error: function () {
    //             $.TeachDialog({
    //                 content: '获取数据失败，无法进行修改',
    //             });
    //             return;
    //         }
    //     });
    // });

    // 编辑

    $('.editbook').click(function () {
        var row = $('#datatable_projecttype').datagrid('getSelections');
        if (row.length<1) {
            $.TeachDialog({
                content: '请选择一行数据进行修改！',
            });
            return;
        }
        if (row.length>1) {
            $.TeachDialog({
                content: '只能选择一行进行修改！',
            });
            return;
        }
        var content = "";
        $.ajax({
            url: 'admin/Project/updateandtype',
            data: "ptid=" + row[0].pt_id,
            type: 'POST',
            dataType: 'HTML',//返回的数据类型
            success: function (updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    title: '修改步骤信息',
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    otherButtons: ['修改'],
                    otherButtonStyles: ['btn-primary'],
                    clickButton: function (sender, modal, index) {
                        $.ajax({
                            url: 'admin/Project/update',
                            data: $("#updateForm").serialize(),
                            type: 'POST',
                            dataType: 'JSON',
                            success: function (returnData) {
                                if (returnData.status == 0) {
                                    $.TeachDialog({
                                        content: returnData.msg
                                    });
                                } else {
                                    modal.modal('hide');
                                    $.TeachDialog({
                                        content: returnData.msg
                                    });
                                    $('#datatable_projecttype').datagrid('reload');
                                }
                            },
                            error: function () {
                                $.TeachDialog({
                                    content: '系统异常，请联系管理员'
                                });
                            },

                        });

                    },
                });
            },
            error: function () {
                $.TeachDialog({
                    content: '系统异常，请联系管理员'
                });
                return;
            }
        });
    })
    /**
     * 删除
     */
    $('.removebook').click(function () {
        var row = $('#datatable_projecttype').datagrid('getSelections');
        if (row.length<1) {
            $.TeachDialog({
                content: '请选择一行数据进行修改！',
            });
            return;
        }
        if (row.length>1) {
            $.TeachDialog({
                content: '只能选择一行进行修改！',
            });
            return;
        }
        $.TeachDialog({
            content: "此操作会删除此步骤下的所有小任务，确认删除吗？",
            showCloseButton: true,
            showCloseButtonName: '取消',
            otherButtons: ['确认'],
            otherButtonStyles: ['btn-danger'],
            clickButton: function (sender, modal, index) {
                modal.modal('hide');
                $.ajax({
                    url: 'admin/Project/delete', //form action
                    dataType: 'JSON', //返回体类型
                    type: 'POST', // form type
                    data: "ptid=" + row[0].pt_id,
                    success: function (data) {
                        if (data.status == 1) {
                            $.TeachDialog({
                                content: data.msg
                            });
                            $('#datatable_projecttype').datagrid('reload');
                            $('#datatable_projecttype').datagrid('uncheckAll');
                        } else {
                            $.TeachDialog({
                                content: '删除失败'
                            });
                        }
                    },
                    error: function () {
                        $.TeachDialog({
                            content: '系统异常，请联系管理员'
                        });
                    }
                });
            }
        });
    })

});
